package ext.tianma.supplier;

import java.rmi.RemoteException;
import java.util.LinkedList;

import wt.enterprise.MadeFromLink;
import wt.fc.Persistable;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.ReferenceFactory;
import wt.fc.WTReference;
import wt.part.WTPart;
import wt.util.WTException;
import ext.com.iba.IBAUtil;
import ext.tianma.version.ComToolUtil;

/**Description:【供应商信息查询】的业务处理逻辑
 * @author    :  YangWen
 * @create at :  2013-8-14   上午10:45:29
 */
public class SupplierInfoQueryBusiness {
	
	private static boolean isShow = true;
	
	/**
	 * 私有构造函数，防止实例化
	 */
	private SupplierInfoQueryBusiness(){
		
	}
	
	/**输出log信息
	 * @param info
	 */
	private static void out(String info){
		if(isShow)
			System.out.println("供应商信息查询:"+SupplierInfoQueryBusiness.class.getName()+":"+info);
	}

	/**YW
	 * 获得指定部件的供应商信息的数据
	 * @param partOid 【部件】的oid值
	 * @return List<WTPart>对象
	 */
	@SuppressWarnings("deprecation")
	public static LinkedList<WTPart> getSupplierInfomationData(String partOid){
		
				
		if(ComToolUtil.isEmpty(partOid))
			return null;
		
		Persistable persistable = getPersistableFromOid(partOid);		
		if(persistable == null || !(persistable instanceof WTPart))
			return null;
		
		out("传入的【部件】是："+persistable.getIdentity());
		
		LinkedList<WTPart> allSave = null;
		
		try {
			allSave = getAllSaveAs((WTPart)persistable);
		} catch (WTException e) {
			System.out.println("获得指定部件的所有另存为对象时出错！");
			e.printStackTrace();
		}
				
		LinkedList<WTPart> filter = filterTrialAndAdvancedversion(allSave, (WTPart)persistable);
		
		out("filter="+filter.size());
		
		return sorting(filter);
	}
	
	
	/**YW
	 * 对指定的链表进行排序：【是否二供】值为否的对象放在最前面
	 * @param partList LinkedList<WTPart>对象
	 * @return LinkedList<WTPart>对象
	 */
	public static LinkedList<WTPart> sorting(LinkedList<WTPart> partList){
		
		if(partList == null || partList.size() == 0)
			return null;
		
		for(int i=0;i<partList.size();i++){
			WTPart one = partList.get(i);						
			Object isSecondSupplier = null;
			try {
				isSecondSupplier = IBAUtil.getIBAValue(one, "isSecondSupplier");
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (WTException e) {
				e.printStackTrace();
			}
			
			if(ComToolUtil.isEmpty(isSecondSupplier) || !(isSecondSupplier instanceof String))
				continue;
			
			String second = (String)isSecondSupplier;
			
			if(second.equals("否")){
				partList.remove(i);
				partList.addFirst(one);
			}
								
		}		
		
		return partList;
	}
	
	
	
	/**YW
	 * 过滤掉【试制】和【版本进阶】两种对象的所有部件
	 * @param partList LinkedList<WTPart>对象
	 * @return LinkedList<WTPart>对象
	 */
	public static LinkedList<WTPart> filterTrialAndAdvancedversion(LinkedList<WTPart> partList , WTPart currentObj){
		if(partList == null || partList.size() == 0)
			return null;
		
		if(ComToolUtil.isEmpty(currentObj))
			return null;
		
		String currentNumber = currentObj.getNumber();
		
		out("当前部件的编号是："+currentNumber);
		out("与当前部件相关的所有另存为部件的个数(包括本身)是："+partList.size());
				
		for(int i=partList.size()-1;i>=0;i--){
			WTPart one = partList.get(i);	
			
			out("某个部件是："+one.getDisplayIdentifier());
			
			String number = one.getNumber();
			
			//过滤掉试制产生的对象【试制的结果是：“另存为编号”以“-T”结尾】
			//过滤掉版本进阶产生的对象【版本进阶的结果是：“另存为编号”和当前部件编号只有最后一位版本位不同】
			if( number.endsWith("-T")||
					(number.length() == currentNumber.length() &&  (number.substring(0, number.length()-1).equals(currentNumber.substring(0, currentNumber.length()-1))) && 
							!(number.substring(number.length()-1).equals(currentNumber.substring(number.length()-1) )))  ){
				partList.remove(i);
				i--;
			}			
		
		}
		
		out("过滤掉【试制】和【版本进阶】后的所有关联部件个数是："+partList.size());
		
		return partList;
	}
	
	
	
	/**YW
	 * 获得指定部件的所有另存为对象，包括：本身、original、copy
	 * @param part WTPart对象
	 * @return List<WTPart>对象
	 * @throws WTException 
	 */
	public static LinkedList<WTPart> getAllSaveAs(WTPart part) throws WTException{
		
		if(ComToolUtil.isEmpty(part))
			return null;
		
		LinkedList<WTPart> result = new LinkedList<WTPart>();
		
		//添加本身
		result.addLast(part);
		
		//添加original
		QueryResult originalQR = PersistenceHelper.manager.navigate(part, "original", MadeFromLink.class, false);
		
		if(originalQR == null)
			out("当前部件【"+part.getDisplayIdentifier()+"】的所有源对象个数为0");
		else
			out("当前部件【"+part.getDisplayIdentifier()+"】的所有源对象个数是："+originalQR.size());
		
		while(originalQR!=null && originalQR.hasMoreElements()) {
			   Object original = originalQR.nextElement();
			   out("当前部件【"+part.getDisplayIdentifier()+"】的所关联的一个源对象："+original);
			   
			   if(original == null || !(original instanceof MadeFromLink))
				  continue;			   
			   MadeFromLink originalLink = (MadeFromLink) original;
			   Object obj = originalLink.getOriginal();			   
			   if(obj == null || !(obj instanceof WTPart))
				   continue;			   
			   WTPart originalPart = (WTPart)obj;
			   
			   out("当前部件【"+part.getDisplayIdentifier()+"】的所关联的一个源对象："+originalPart.getDisplayIdentifier());
			   result.addLast(originalPart);
    	}	
		
		//添加original  
		QueryResult copyQR = PersistenceHelper.manager.navigate(part, "copy", MadeFromLink.class, false);
		
		if(copyQR == null)
			out("当前部件【"+part.getDisplayIdentifier()+"】的所有目标对象个数为0");
		else
			out("当前部件【"+part.getDisplayIdentifier()+"】的所有目标对象个数是："+copyQR.size());
		
		while(copyQR!=null && copyQR.hasMoreElements()) {
			   Object copy = copyQR.nextElement();
			   out("当前部件【"+part.getDisplayIdentifier()+"】的所关联的一个目标对象："+copy);
			   
			   if(copy == null || !(copy instanceof MadeFromLink))
				  continue;			   
			   MadeFromLink copyLink = (MadeFromLink) copy;
			   Object obj = copyLink.getCopy();			   
			   if(obj == null || !(obj instanceof WTPart))
				   continue;			   
			   WTPart copyPart = (WTPart)obj;
				   
			   out("当前部件【"+part.getDisplayIdentifier()+"】的所关联的一个目标对象："+copyPart.getDisplayIdentifier());
			   result.addLast(copyPart);
    	}
		
		out("当前部件【"+part.getDisplayIdentifier()+"】的所有关联的【源、目标、自身】对象个数是："+result.size());
		
		return result;
	}
	
	
	
	
    /**YW
     * 通过oid来获得对象
     * @param oid  对象的oid
     * @return oid所对应的对象
     */
    public static Persistable getPersistableFromOid(String oid) {
    	if(ComToolUtil.isEmpty(oid))
    		return null;
    	
		try {
		    ReferenceFactory refFactory = new ReferenceFactory();
		    WTReference reference = refFactory.getReference(oid);
		    if (reference == null) 
		    	return null;	    
		    return reference.getObject();
		} catch (WTException e) {
		    e.getMessage();
		}
		
		return null;
    }
	
}
